import {getDic} from "@/api/system/dict.ts";



/**
 * 生成字典
 * @param dicts string || array
 * @returns {Object}
 */
export const createDict = async (dictList:any) => {
    //如果是dicts:String类型，改成Array类型
    if (typeof dictList === "string") dictList = dictList.split('')
    let result:any= {
        dictIndex:[], //索引
        dictList:[] //字典内容
    }
    for (let i in dictList){
        result.dictIndex.push(dictList[i])
        result.dictList.push(await getDic(dictList[i]))
    }
    return result
}

/**
 * 字典值转HTML标签
 * @param dictKey 要转换的字典名称
 * @param value 字典值
 * @param dictIndex 字典值索引
 * @param dictList 字典内容
 */
export const dictKeyToValue = (dictKey:string, value, dictIndex:Array<string>=[], dictList=[]) => {
    const index = dictIndex.indexOf(dictKey)
    const dict: any = dictList[index]
    for (let i in dict){
        if (dict[i].dictValue == value){
            return `<span class="dict-tag ${dict[i].listClass}">${dict[i].dictLabel}</span>`
        }
    }
    return `<span class="dict-tag danger">字典值错误</span>`
}


export const dictKeyToSelect = (dictKey:string, dictIndex:Array<string>=[], dictList=[]) => {
    const index = dictIndex.indexOf(dictKey)
    const dict = dictList[index]
    return dict
}